package com.qin.mall.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.qin.mall.common.api.CommonResult;
import com.qin.mall.common.api.MyBatisPlusPage;
import com.qin.mall.domain.PmsProductAttributeCategory;
import com.qin.mall.service.PmsProductAttributeCategoryService;
import com.qin.mall.dto.PmsProductAttributeCategoryItem;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 产品属性结合分类Controller
 * @author Leisure
 * create 2022 2022/11/6 21:05
 **/
@RestController
@RequestMapping("/productAttribute/category")
public class ProductAttributeCategoryController {
    
    @Autowired
    private PmsProductAttributeCategoryService productAttributeCategoryService;
    
    //创建属性分类
    @PostMapping("/create$")
    public CommonResult create(@RequestParam("name") String name){
        int count = productAttributeCategoryService.create(name);
        if (count > 0) {
            return CommonResult.success(count);
        } else {
            return CommonResult.failed();
        }
    }

    //TODO 修改属性分类
    @PostMapping("/update$/{id}")
    public CommonResult update(@PathVariable Long id, @RequestParam("name") String name){
        int count = productAttributeCategoryService.update(id, name);
        if (count > 0) {
            return CommonResult.success(count);
        } else {
            return CommonResult.failed();
        }
    }

    //TODO 删除属性分类
    @GetMapping("/delete$/{id}")
    public CommonResult deleteById(@PathVariable Long id){
        int count = productAttributeCategoryService.delete(id);
        if (count > 0) {
            return CommonResult.success(count);
        } else {
            return CommonResult.failed();
        }
    }

    //TODO 获取单个属性分类
    @GetMapping("$/")
    public CommonResult selectById(@RequestParam("id") Long id){
        PmsProductAttributeCategory result = productAttributeCategoryService.getItem(id);
        return CommonResult.success(result);
    }

    //TODO 分页查询属性分类
    @GetMapping("/list$")
    public CommonResult<MyBatisPlusPage<PmsProductAttributeCategory>> selectPage(@RequestParam(value = "pageNum",defaultValue = "1") Integer pageNum,
                                                                                 @RequestParam(value = "pageSize",defaultValue = "5") Integer pageSize){
        Page<PmsProductAttributeCategory> list = productAttributeCategoryService.getList(pageNum, pageSize);
        return CommonResult.success(MyBatisPlusPage.mpPage(list));
    }
    
    //TODO 获取包含属性的属性分类、分类属性一起查出来
    @GetMapping("/list/withAttr$")
    public CommonResult<List<PmsProductAttributeCategoryItem>> getListWithAttr(){
        List<PmsProductAttributeCategoryItem> list = productAttributeCategoryService.getListWithAttr();
        return CommonResult.success(list);
    }

}
